<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="商品编号" prop="productNo">
      <el-input v-model="dataForm.productNo" placeholder="商品编号"></el-input>
    </el-form-item>
    <el-form-item label="商品名称" prop="productName">
      <el-input v-model="dataForm.productName" placeholder="商品名称"></el-input>
    </el-form-item>
    <el-form-item label="商品价格" prop="productPrice">
      <el-input v-model="dataForm.productPrice" placeholder="商品价格"></el-input>
    </el-form-item>
    <el-form-item label="分类" prop="categoryId">
      <category-cascader :catelogPath.sync="categoryPath"></category-cascader>
    </el-form-item>
    <el-form-item label="库存" prop="stock">
      <el-input v-model="dataForm.stock" placeholder="库存"></el-input>
    </el-form-item>
    <el-form-item label="商品描述" prop="describes">
      <el-input v-model="dataForm.describes" placeholder="商品描述"></el-input>
    </el-form-item>
    <el-form-item label="商品详细描述" prop="detail">
      <el-input v-model="dataForm.detail" placeholder="商品详细描述"></el-input>
    </el-form-item>
    <el-form-item label="商品图片url" prop="picture">
      <single-upload v-model="dataForm.avatar"></single-upload>
    </el-form-item>
    <el-form-item label="是否热销" prop="isHotSell">
      <template slot-scope="scope">
        <el-switch
          v-model="dataForm.isHotSell"
          active-color="#13ce66"
          inactive-color="#ff4949"
          :active-value="1"
          :inactive-value="0"
        ></el-switch>
      </template>
    </el-form-item>
    <el-form-item label="是否禁用" prop="isDisable">
      <template slot-scope="scope">
        <el-switch
          v-model="dataForm.isDisable"
          active-color="#13ce66"
          inactive-color="#ff4949"
          :active-value="1"
          :inactive-value="0"
        ></el-switch>
      </template>
    </el-form-item>

    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  import singleUpload from '@/components/upload/singleUpload';
  import CategoryCascader from '../common/category-cascader'
  export default {
    components: {singleUpload,CategoryCascader},
    data () {
      return {
        visible: false,
        catPathSub: null,
        categoryPath: [],
        dataForm: {
          id: 0,
          productNo: '',
          productName: '',
          productPrice: '',
          categoryId: '',
          stock: '',
          describes: '',
          detail: '',
          picture: '',
          monthSellCount: '',
          isHotSell: '',
          collectionCount: '',
          isDisable: '',
          createTime: '',
          updateTime: '',
        },
        dataRule: {
          productNo: [
            { required: true, message: '商品编号不能为空', trigger: 'blur' }
          ],
          productName: [
            { required: true, message: '商品名称不能为空', trigger: 'blur' }
          ],
          productPrice: [
            { required: true, message: '商品价格不能为空', trigger: 'blur' }
          ],
          categoryId: [
            { required: true, message: '分类id不能为空', trigger: 'blur' }
          ],
          stock: [
            { required: true, message: '库存不能为空', trigger: 'blur' }
          ],
          describes: [
            { required: true, message: '商品描述不能为空', trigger: 'blur' }
          ],
          detail: [
            { required: true, message: '商品详细描述不能为空', trigger: 'blur' }
          ],
          picture: [
            { required: true, message: '商品图片url不能为空', trigger: 'blur' }
          ],
          monthSellCount: [
            { required: true, message: '月销量不能为空', trigger: 'blur' }
          ],
          isHotSell: [
            { required: true, message: '是否热销(0:是 1：否)不能为空', trigger: 'blur' }
          ],
          collectionCount: [
            { required: true, message: '收藏量不能为空', trigger: 'blur' }
          ],
          isDisable: [
            { required: true, message: '是否禁用(0:是 1：否)不能为空', trigger: 'blur' }
          ],
          createTime: [
            { required: true, message: '创建时间不能为空', trigger: 'blur' }
          ],
          updateTime: [
            { required: true, message: '更新时间不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornApi(`/product/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.productNo = data.product.productNo
                this.dataForm.productName = data.product.productName
                this.dataForm.productPrice = data.product.productPrice
                this.dataForm.categoryId = data.product.categoryId
                this.dataForm.stock = data.product.stock
                this.dataForm.describes = data.product.describes
                this.dataForm.detail = data.product.detail
                this.dataForm.picture = data.product.picture
                this.dataForm.monthSellCount = data.product.monthSellCount
                this.dataForm.isHotSell = data.product.isHotSell
                this.dataForm.collectionCount = data.product.collectionCount
                this.dataForm.isDisable = data.product.isDisable
                this.dataForm.createTime = data.product.createTime
                this.dataForm.updateTime = data.product.updateTime
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornApi(`/product/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'productNo': this.dataForm.productNo,
                'productName': this.dataForm.productName,
                'productPrice': this.dataForm.productPrice,
                'categoryId': this.dataForm.categoryId,
                'stock': this.dataForm.stock,
                'describes': this.dataForm.describes,
                'detail': this.dataForm.detail,
                'picture': this.dataForm.picture,
                'monthSellCount': this.dataForm.monthSellCount,
                'isHotSell': this.dataForm.isHotSell,
                'collectionCount': this.dataForm.collectionCount,
                'isDisable': this.dataForm.isDisable,
                'createTime': this.dataForm.createTime,
                'updateTime': this.dataForm.updateTime
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
